<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestAd extends PHPUnit_Framework_TestCase {

	function test_get_edit_everything() {
		$ad_id = 45;
		$page = new Page();
		$ad = new Ad($page, $ad_id);

		$expected = array("TestResult");

		$db_access = $this->getMock("DbAccessor");

		$db_access->expects($this->once())
				  ->method("db_query_select")
				  ->with(array(array('*')), array("AD"), "item_id = {$ad_id}")
				  ->will($this->returnValue("TestSqlResult"));

		$db_access->expects($this->once())
				  ->method("db_do_query")
				  ->with("TestSqlResult")
				  ->will($this->returnValue("TestDbResult"));

		$db_access->expects($this->once())
				  ->method("db_fetch_array")
				  ->with("TestDbResult")
				  ->will($this->returnValue($expected));

		$ad->set_db_accessor($db_access);

		$this->assertEquals($expected, $ad->get_edit_everything());
	}

	function test_check_content_with_no_new_game_all_validate() {
		$values = array("game_id" => 5,
						"game_type" => "type_of_game",
						"region" => 37);
		
		$page = new Page();
		$ad = new Ad($page);

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->at(0))
				->method("checkBody")
				->with($values)
				->will($this->returnValue(true));

		$checker->expects($this->at(1))
				->method("check_int")
				->with($values["game_id"], "spel")
				->will($this->returnValue(true));

		$checker->expects($this->at(2))
				->method("check_input")
				->with($values["game_id"] > 0, "spel niet ingevuld")
				->will($this->returnValue(true));

		$checker->expects($this->at(3))
				->method("check_input")
				->with($values["region"] > 0, "regio niet aangegeven")
				->will($this->returnValue(true));

		$checker->expects($this->never())
				->method("check_exists");

		$checker->expects($this->never())
				->method("check_tags");

		$ad->set_input_checker($checker);

		$this->assertTrue($ad->check_content($values));
	}

	function test_check_content_with_no_new_game_first_doesnt_validate() {
		$values = array("game_id" => 5,
						"game_type" => "type_of_game",
						"region" => 37);
		
		$page = new Page();
		$ad = new Ad($page);

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->at(0))
				->method("checkBody")
				->with($values)
				->will($this->returnValue(false));

		$checker->expects($this->never())
				->method("check_int");

		$checker->expects($this->never())
				->method("check_input");

		$checker->expects($this->never())
				->method("check_exists");

		$checker->expects($this->never())
				->method("check_tags");

		$ad->set_input_checker($checker);

		$this->assertFalse($ad->check_content($values));
	}

	function test_check_content_with_no_new_game_second_doesnt_validate() {
		$values = array("game_id" => 5,
						"game_type" => "type_of_game",
						"region" => 37);
		
		$page = new Page();
		$ad = new Ad($page);

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->at(0))
				->method("checkBody")
				->with($values)
				->will($this->returnValue(true));

		$checker->expects($this->at(1))
				->method("check_int")
				->with($values["game_id"], "spel")
				->will($this->returnValue(false));

		$checker->expects($this->never())
				->method("check_input");

		$checker->expects($this->never())
				->method("check_exists");

		$checker->expects($this->never())
				->method("check_tags");

		$ad->set_input_checker($checker);

		$this->assertFalse($ad->check_content($values));
	}

	function test_check_content_with_no_new_game_third_doesnt_validate() {
		$values = array("game_id" => 5,
						"game_type" => "type_of_game",
						"region" => 37);
		
		$page = new Page();
		$ad = new Ad($page);

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->at(0))
				->method("checkBody")
				->with($values)
				->will($this->returnValue(true));

		$checker->expects($this->at(1))
				->method("check_int")
				->with($values["game_id"], "spel")
				->will($this->returnValue(true));

		$checker->expects($this->at(2))
				->method("check_input")
				->with($values["game_id"] > 0, "spel niet ingevuld")
				->will($this->returnValue(false));

		$checker->expects($this->never())
				->method("check_exists");

		$checker->expects($this->never())
				->method("check_tags");

		$ad->set_input_checker($checker);

		$this->assertFalse($ad->check_content($values));
	}

	function test_check_content_with_no_new_game_fourth_doesnt_validate() {
		$values = array("game_id" => 5,
						"game_type" => "type_of_game",
						"region" => 37);
		
		$page = new Page();
		$ad = new Ad($page);

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->at(0))
				->method("checkBody")
				->with($values)
				->will($this->returnValue(true));

		$checker->expects($this->at(1))
				->method("check_int")
				->with($values["game_id"], "spel")
				->will($this->returnValue(true));

		$checker->expects($this->at(2))
				->method("check_input")
				->with($values["game_id"] > 0, "spel niet ingevuld")
				->will($this->returnValue(true));

		$checker->expects($this->at(3))
				->method("check_input")
				->with($values["region"] > 0, "regio niet aangegeven")
				->will($this->returnValue(false));

		$checker->expects($this->never())
				->method("check_exists");

		$checker->expects($this->never())
				->method("check_tags");

		$ad->set_input_checker($checker);

		$this->assertFalse($ad->check_content($values));
	}

	function test_check_content_with_new_game_all_validate() {
		$values = array("game_id" => "NEW",
						"game_name" => "best game ever",
						"game_type" => "type_of_game",
						"region" => 37);
		
		$page = new Page();
		$ad = new Ad($page);

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->at(0))
				->method("checkBody")
				->with($values)
				->will($this->returnValue(true));

		$checker->expects($this->at(1))
				->method("check_int")
				->with($values["game_type"], "spel - soort")
				->will($this->returnValue(true));

		$checker->expects($this->at(2))
				->method("check_input")
				->with($values["game_type"], "spel - soort niet ingevuld")
				->will($this->returnValue(true));

		$checker->expects($this->at(3))
				->method("check_exists")
				->with($values["game_name"], "spel - naam")
				->will($this->returnValue(true));

		$checker->expects($this->at(4))
				->method("check_tags")
				->with($values["game_name"], "spel - naam")
				->will($this->returnValue(true));

		$checker->expects($this->at(5))
				->method("check_input")
				->with($values["region"] > 0, "regio niet aangegeven")
				->will($this->returnValue(true));

		$ad->set_input_checker($checker);

		$this->assertTrue($ad->check_content($values));
	}

	function test_check_content_with_no_new_game_all_validate_and_region_filled() {
		$values = array("game_id" => 5,
						"game_type" => "type_of_game",
						"region" => "parent37",
						"region_name" => "region name");
		
		$page = new Page();
		$ad = new Ad($page);

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->at(0))
				->method("checkBody")
				->with($values)
				->will($this->returnValue(true));

		$checker->expects($this->at(1))
				->method("check_int")
				->with($values["game_id"], "spel")
				->will($this->returnValue(true));

		$checker->expects($this->at(2))
				->method("check_input")
				->with($values["game_id"] > 0, "spel niet ingevuld")
				->will($this->returnValue(true));

		$checker->expects($this->at(3))
				->method("check_exists")
				->with($values["region_name"], "naam regio")
				->will($this->returnValue(true));

		$checker->expects($this->at(4))
				->method("check_tags")
				->with($values["region_name"], "naam regio")
				->will($this->returnValue(true));

		$ad->set_input_checker($checker);

		$this->assertTrue($ad->check_content($values));
	}

	function test_derive_info_with_basic_info() {
		$expected_post_region = "post region";
		$_POST['region'] = $expected_post_region;
		$expected_region_tree_output = 'region tree output';
		$expected_formatted_region = 'formatted region';

		$page = new Page();
		$ad = new AdTestClass($page);

		$manager = $this->getMock("RegionManager");
		$manager->expects($this->once())
				->method('region_get_region_tree')
				->with($expected_post_region)
				->will($this->returnValue($expected_region_tree_output));

		$manager->expects($this->once())
				->method('region_format')
				->with($expected_region_tree_output)
				->will($this->returnValue($expected_formatted_region));

		$ad->set_region_manager($manager);

		$actual = $ad->derive_info_test_function();
		$this->assertEquals($expected_formatted_region, $actual);
	}

	function test_derive_info_with_basic_info_and_post_game_name() {
		$expected_post_region = "post region";
		$expected_game_name = 'monopoly';
		$_POST['region'] = $expected_post_region;
		$_POST['game_name'] = $expected_game_name;
		$expected_region_tree_output = 'region tree output';
		$expected_formatted_region = 'formatted region';

		$page = new Page();
		$ad = new AdTestClass($page);

		$manager = $this->getMock("RegionManager");
		$manager->expects($this->once())
				->method('region_get_region_tree')
				->with($expected_post_region)
				->will($this->returnValue($expected_region_tree_output));

		$manager->expects($this->once())
				->method('region_format')
				->with($expected_region_tree_output)
				->will($this->returnValue($expected_formatted_region));

		$ad->set_region_manager($manager);

		$actual = $ad->derive_info_test_function();
		$this->assertEquals("$expected_game_name - $expected_formatted_region", $actual);
	}

	function test_derive_info_with_basic_info_and_post_game_id_no_returned_rows() {
		$expected_post_region = "post region";
		$expected_game_id = '35';
		$_POST['region'] = $expected_post_region;
		$_POST['game_id'] = $expected_game_id;
		$expected_region_tree_output = 'region tree output';
		$expected_formatted_region = 'formatted region';
		$expected_select_query = 'select query';
		$expected_sql_output = 'sql output';

		$page = new Page();
		$ad = new AdTestClass($page);

		$manager = $this->getMock("RegionManager");
		$manager->expects($this->once())
				->method('region_get_region_tree')
				->with($expected_post_region)
				->will($this->returnValue($expected_region_tree_output));

		$manager->expects($this->once())
				->method('region_format')
				->with($expected_region_tree_output)
				->will($this->returnValue($expected_formatted_region));

		$db_access = $this->getMock('DbAccessor');
		$db_access->expects($this->once())
				->method('db_query_select')
				->with(array(array('name')), array('GAME'), "game_id = $expected_game_id")
				->will($this->returnValue($expected_select_query));

		$db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_select_query)
				->will($this->returnValue($expected_sql_output));
		$db_access->expects($this->once())
				->method('db_num_rows')
				->with($expected_sql_output)
				->will($this->returnValue(0));

		$ad->set_db_accessor($db_access);
		$ad->set_region_manager($manager);

		$actual = $ad->derive_info_test_function();
		$this->assertEquals("$expected_formatted_region", $actual);
	}

	function test_derive_info_with_basic_info_and_post_game_id_1_returned_row() {
		$expected_post_region = "post region";
		$expected_game_id = '35';
		$_POST['region'] = $expected_post_region;
		$_POST['game_id'] = $expected_game_id;
		$expected_region_tree_output = 'region tree output';
		$expected_formatted_region = 'formatted region';
		$expected_select_query = 'select query';
		$expected_sql_output = 'sql output';
		$expected_game = array('name' => 'monopoly');

		$page = new Page();
		$ad = new AdTestClass($page);

		$manager = $this->getMock("RegionManager");
		$manager->expects($this->once())
				->method('region_get_region_tree')
				->with($expected_post_region)
				->will($this->returnValue($expected_region_tree_output));

		$manager->expects($this->once())
				->method('region_format')
				->with($expected_region_tree_output)
				->will($this->returnValue($expected_formatted_region));

		$db_access = $this->getMock('DbAccessor');
		$db_access->expects($this->once())
				->method('db_query_select')
				->with(array(array('name')), array('GAME'), "game_id = $expected_game_id")
				->will($this->returnValue($expected_select_query));

		$db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_select_query)
				->will($this->returnValue($expected_sql_output));
		$db_access->expects($this->once())
				->method('db_num_rows')
				->with($expected_sql_output)
				->will($this->returnValue(1));
		$db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_output)
				->will($this->returnValue($expected_game));

		$ad->set_db_accessor($db_access);
		$ad->set_region_manager($manager);

		$actual = $ad->derive_info_test_function();
		$this->assertEquals("{$expected_game['name']} - $expected_formatted_region", $actual);
	}

	function test_get_sql_from_post_existing_region_and_game() {
		$expected_region_post_value = 'region_id';
		$expected_game_post_value = 'game_id';
		$expected_item_id = 'item_id';
		$expected_body_post_value = 'body_value';
		$expected_seek_post_value = 'GZ';
		$expected_body_sqlized_value = 'sqlized_body_value';
		$expected_query_insert = 'query_insert';

		$_POST['region'] = $expected_region_post_value;
		$_POST['game_id'] = $expected_game_post_value;
		$_POST['body'] = $expected_body_post_value;
		$_POST['seek'] = $expected_seek_post_value;

		$page = new Page();
		$ad = new AdTestClass($page);
		
		$db_access = $this->getMock('DbAccessor');
		$db_access->expects($this->once())
				->method('db_sqlize')
				->with($expected_body_post_value)
				->will($this->returnValue($expected_body_sqlized_value));
		$db_access->expects($this->once())
				->method('db_query_insert')
				->with(array('item_id', 'game_id', 'region_id', 'body', 'seek'), 
						array($expected_item_id, $expected_game_post_value, 
								$expected_region_post_value, $expected_body_sqlized_value, 
								1), 
						"AD")
				->will($this->returnValue($expected_query_insert));
		$ad->set_db_accessor($db_access);

		$actual = $ad->get_sql_from_post_test_function($expected_item_id);
		$this->assertEquals($expected_query_insert, $actual);
	}

	function test_get_sql_from_post_existing_region_and_game_item_not_sought() {
		$expected_region_post_value = 'region_id';
		$expected_game_post_value = 'game_id';
		$expected_item_id = 'item_id';
		$expected_body_post_value = 'body_value';
		$expected_seek_post_value = 'not_GZ';
		$expected_body_sqlized_value = 'sqlized_body_value';
		$expected_query_insert = 'query_insert';

		$_POST['region'] = $expected_region_post_value;
		$_POST['game_id'] = $expected_game_post_value;
		$_POST['body'] = $expected_body_post_value;
		$_POST['seek'] = $expected_seek_post_value;

		$page = new Page();
		$ad = new AdTestClass($page);
		
		$db_access = $this->getMock('DbAccessor');
		$db_access->expects($this->once())
				->method('db_sqlize')
				->with($expected_body_post_value)
				->will($this->returnValue($expected_body_sqlized_value));
		$db_access->expects($this->once())
				->method('db_query_insert')
				->with(array('item_id', 'game_id', 'region_id', 'body', 'seek'), 
						array($expected_item_id, $expected_game_post_value, 
								$expected_region_post_value, $expected_body_sqlized_value, 
								0), 
						"AD")
				->will($this->returnValue($expected_query_insert));
		$ad->set_db_accessor($db_access);

		$actual = $ad->get_sql_from_post_test_function($expected_item_id);
		$this->assertEquals($expected_query_insert, $actual);
	}

	function test_get_sql_from_post_existing_region_and_no_game() {
		$expected_region_post_value = 'region_id';
		$expected_game_post_value = 'NONE';
		$expected_item_id = 'item_id';
		$expected_body_post_value = 'body_value';
		$expected_seek_post_value = 'GZ';
		$expected_body_sqlized_value = 'sqlized_body_value';
		$expected_query_insert = 'query_insert';

		$_POST['region'] = $expected_region_post_value;
		$_POST['game_id'] = $expected_game_post_value;
		$_POST['body'] = $expected_body_post_value;
		$_POST['seek'] = $expected_seek_post_value;

		$page = new Page();
		$ad = new AdTestClass($page);
		
		$db_access = $this->getMock('DbAccessor');
		$db_access->expects($this->once())
				->method('db_sqlize')
				->with($expected_body_post_value)
				->will($this->returnValue($expected_body_sqlized_value));
		$db_access->expects($this->once())
				->method('db_query_insert')
				->with(array('item_id', 'game_id', 'region_id', 'body', 'seek'), 
						array($expected_item_id, 'NULL', 
								$expected_region_post_value, $expected_body_sqlized_value, 
								1), 
						"AD")
				->will($this->returnValue($expected_query_insert));
		$ad->set_db_accessor($db_access);

		$actual = $ad->get_sql_from_post_test_function($expected_item_id);
		$this->assertEquals($expected_query_insert, $actual);
	}

	function test_get_sql_from_post_existing_region_and_new_game() {
		$expected_region_post_value = 'region_id';
		$expected_game_post_value = 'NEW';
		$expected_game_name_post_value = 'game_name';
		$expected_item_id = 'item_id';
		$expected_body_post_value = 'body_value';
		$expected_seek_post_value = 'GZ';
		$expected_body_sqlized_value = 'sqlized_body_value';
		$expected_query_insert = 'query_insert';
		$expected_desqlized_game = 'desqlized_game';
		$expected_query_game_insert = 'query_insert_game';
		$expected_query_game_type_insert = 'query_insert_game_type';
		$expected_insert_id = 'insert_id';

		$_POST['region'] = $expected_region_post_value;
		$_POST['game_id'] = $expected_game_post_value;
		$_POST['body'] = $expected_body_post_value;
		$_POST['seek'] = $expected_seek_post_value;
		$_POST['game_type'] = $expected_game_type_post_value;
		$_POST['game_name'] = $expected_game_name_post_value;

		$page = new Page();
		$ad = new AdTestClass($page);
		
		$db_access = $this->getMock('DbAccessor');

		$db_access->expects($this->at(0))
				->method('db_sqlize')
				->with($expected_game_name_post_value)
				->will($this->returnValue($expected_desqlized_game));
		$db_access->expects($this->at(1))
				->method('db_query_insert')
				->with(array('name'), array($expected_desqlized_game), 'GAME')
				->will($this->returnValue($expected_query_game_insert));
		$db_access->expects($this->at(2))
				->method('db_do_query')
				->with($expected_query_game_insert);

		$db_access->expects($this->at(3))
				->method('db_insert_id')
				->with()
				->will($this->returnValue($expected_insert_id));

		$db_access->expects($this->at(4))
				->method('db_query_insert')
				->with(array('game_id', 'game_type_id'), 
						array($expected_insert_id, $expected_game_type_post_value), 
						"GAME_OF_TYPE")
				->will($this->returnValue($expected_query_game_type_insert));

		$db_access->expects($this->at(5))
				->method('db_do_query')
				->with($expected_query_game_type_insert);

		$db_access->expects($this->at(6))
				->method('db_sqlize')
				->with($expected_body_post_value)
				->will($this->returnValue($expected_body_sqlized_value));
		$db_access->expects($this->at(7))
				->method('db_query_insert')
				->with(array('item_id', 'game_id', 'region_id', 'body', 'seek'), 
						array($expected_item_id, $expected_insert_id, 
								$expected_region_post_value, $expected_body_sqlized_value, 
								1), 
						"AD")
				->will($this->returnValue($expected_query_insert));
		$ad->set_db_accessor($db_access);

		$actual = $ad->get_sql_from_post_test_function($expected_item_id);
		$this->assertEquals($expected_query_insert, $actual);
	}

	function test_get_sql_from_post_new_region_and_no_game() {
		$expected_region_post_value = 'parent1';
		$expected_region_name_post_value = 'region_name';
		$expected_sqlized_region_name = 'sqlized_region_name';
		$expected_region_name_insert_query = 'region_name_insert_query';
		$expected_region_tree_insert_query = 'region_tree_insert_query';
		$expected_region_id = 7;
		$expected_game_post_value = 'NONE';
		$expected_item_id = 'item_id';
		$expected_body_post_value = 'body_value';
		$expected_seek_post_value = 'GZ';
		$expected_body_sqlized_value = 'sqlized_body_value';
		$expected_query_insert = 'query_insert';

		$_POST['region'] = $expected_region_post_value;
		$_POST['region_name'] = $expected_region_name_post_value;
		$_POST['game_id'] = $expected_game_post_value;
		$_POST['body'] = $expected_body_post_value;
		$_POST['seek'] = $expected_seek_post_value;

		$page = new Page();
		$ad = new AdTestClass($page);
		
		$db_access = $this->getMock('DbAccessor');
		$db_access->expects($this->at(0))
				->method('db_sqlize')
				->with($expected_region_name_post_value)
				->will($this->returnValue($expected_sqlized_region_name));
		$db_access->expects($this->at(1))
				->method('db_query_insert')
				->with(array('name'), 
						array($expected_sqlized_region_name), 
						"REGION")
				->will($this->returnValue($expected_region_name_insert_query));
		$db_access->expects($this->at(2))
				->method('db_do_query')
				->with($expected_region_name_insert_query);
		$db_access->expects($this->at(3))
				->method('db_insert_id')
				->will($this->returnValue($expected_region_id));
		$db_access->expects($this->at(4))
				->method('db_query_insert')
				->with(array('parent_region', 'sub_region'), 
						array(substr($expected_region_post_value, 6), $expected_region_id), 
						"REGION_TREE")
				->will($this->returnValue($expected_region_tree_insert_query));
		$db_access->expects($this->at(5))
				->method('db_do_query')
				->with($expected_region_tree_insert_query);
		$db_access->expects($this->at(6))
				->method('db_sqlize')
				->with($expected_body_post_value)
				->will($this->returnValue($expected_body_sqlized_value));
		$db_access->expects($this->at(7))
				->method('db_query_insert')
				->with(array('item_id', 'game_id', 'region_id', 'body', 'seek'), 
						array($expected_item_id, 'NULL', 
								$expected_region_id, $expected_body_sqlized_value, 
								1), 
						"AD")
				->will($this->returnValue($expected_query_insert));
		$ad->set_db_accessor($db_access);

		$actual = $ad->get_sql_from_post_test_function($expected_item_id);
		$this->assertEquals($expected_query_insert, $actual);
	}

	function test_prepare() {
		$expected_kv_games = array('keys' => array(),
				'values' => array());

		$expected_inputs = array();
		$expected_desqlized_item_body = 'desqlized_item_body';
		$expected_item_body = 'item_body';
		$expected_item_title = 'item_title';
		$expected_title_input = 'title_input';
		$expected_info_input = 'info_input';
		$expected_search_offer_input = 'search_offer_input';
		$expected_item_name_input = 'item_name_input';
		$expected_example_input = 'example_input';
		$expected_game_id_input = 'game_id_input';
		$expected_game_kv_keys = array('NONE', 'NEW');
		$expected_game_kv_values = array('GEEN', 'NIEUW SPEL');
		$expected_item_region_id = 5;
		$expected_kv_regions_keys = array('region_keys');
		$expected_kv_regions_values = array('region_values');
		$expected_kv_regions = array('keys' => $expected_kv_regions_keys,
				'values' => $expected_kv_regions_values);
		$expected_region_input  = 'region_input';

		$expected_item = array('body' => $expected_item_body,
				'title' => $expected_item_title,
				'region_id' => $expected_item_region_id);

		$expected_description_input = 'description_input';

		$expected_inputs = array($expected_title_input,
				$expected_info_input,
				$expected_search_offer_input,
				$expected_item_name_input,
				$expected_example_input,
				$expected_game_id_input,
				$expected_region_input,
				$expected_description_input);

		$page = new Page();
		$ad = new Ad($page);

		$mock_toolbox = $this->getMock('Toolbox');
		$mock_toolbox->expects($this->once())
				->method('get_keys_and_values')
				->with("game_type_id", "game_type", "GAME_TYPE")
				->will($this->returnValue(array('keys' => null, 'values' => null)));
		$ad->set_toolbox($mock_toolbox);

		$mock_game_manager = $this->getMock('GameManager');
		$mock_game_manager->expects($this->once())
				->method('game_get_games_kv')
				->will($this->returnValue($expected_kv_games));
		$ad->set_game_manager($mock_game_manager);

		$mock_region_manager = $this->getMock('RegionManager');
		$mock_region_manager->expects($this->once())
				->method('region_get_regions_kv')
				->with()
				->will($this->returnValue($expected_kv_regions));
		$ad->set_region_manager($mock_region_manager);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_desqlize')
				->with($expected_item_body)
				->will($this->returnValue($expected_desqlized_item_body));
		$ad->set_db_accessor($mock_db_access);

		$mock_form_creator = $this->getMock('FormCreator');
		$mock_form_creator->expects($this->at(0))
				->method('input_field')
				->with("Titel", "title", $expected_item_title, 20, 100, "", 1)
				->will($this->returnValue($expected_title_input));
		$mock_form_creator->expects($this->at(1))
				->method('info')
				->with("De titel wordt automatisch ingevuld.\n" .
						"		<script type=\"text/javaScript\">\n" .
						"			function setTitle() {\n" .
						"				if (document.itemform.seek[0].checked)\n" .
						"					var str = 'GZ';\n" . 
						"				else\n" .
						"					var str = 'TK';\n" .
						"				str += ': ' + document.itemform.what.value;\n" .
						"				document.itemform.title.value = str;\n" .
						"			}\n" .
						"		</script>")
				->will($this->returnValue($expected_info_input));
		$mock_form_creator->expects($this->at(2))
				->method('input_radio')
				->with("", "seek", array("GZ","TK"), array("zoeken", "aanbieden"), "GZ", "setTitle()")
				->will($this->returnValue($expected_search_offer_input));
		$mock_form_creator->expects($this->at(3))
				->method('input_field')
				->with("Wat", "what", substr($expected_item_title, 4), 20, 100, "setTitle()")
				->will($this->returnValue($expected_item_name_input));
		$mock_form_creator->expects($this->at(4))
				->method('info')
				->with("bvb: 'spelers', 'regelboek 4e editie', 'dobbelstenen' ...")
				->will($this->returnValue($expected_example_input));
		$mock_form_creator->expects($this->at(5))
				->method('input_select')
				->with("Spel", 
						"game_id", 
						$expected_game_kv_keys, 
						$expected_game_kv_values, 
						"kies een spel", 
						"NONE", 
						"document.itemform.submit()")
				->will($this->returnValue($expected_game_id_input));
		$mock_form_creator->expects($this->at(6))
				->method('input_select')
				->with("Regio", "region", 
					array_merge(array("NEW"), $expected_kv_regions_keys), 
					array_merge(array("NIEUWE REGIO"), $expected_kv_regions_values), 
					"kies een regio", 
					$expected_item_region_id, 
					"document.itemform.submit()")
				->will($this->returnValue($expected_region_input));
		$mock_form_creator->expects($this->at(7))
				->method('input_text')
				->with("Beschrijving", "body", $expected_desqlized_item_body)
				->will($this->returnValue($expected_description_input));
		$ad->set_form_creator($mock_form_creator);
		
		$actual = $ad->prepare(null, $expected_item, array());
		$this->assertEquals($expected_inputs, $actual);
	}

	function test_prepare_with_new_game() {
		$expected_kv_games = array('keys' => array(),
				'values' => array());

		$expected_inputs = array();
		$expected_desqlized_item_body = 'desqlized_item_body';
		$expected_item_body = 'item_body';
		$expected_item_title = 'item_title';
		$expected_title_input = 'title_input';
		$expected_info_input = 'info_input';
		$expected_search_offer_input = 'search_offer_input';
		$expected_item_name_input = 'item_name_input';
		$expected_example_input = 'example_input';
		$expected_game_id_input = 'game_id_input';
		$expected_game_id_post = "NEW";
		$expected_game_kv_keys = array('NONE', 'NEW');
		$expected_game_kv_values = array('GEEN', 'NIEUW SPEL');
		$expected_item_region_id = 5;
		$expected_kv_regions_keys = array('region_keys');
		$expected_kv_regions_values = array('region_values');
		$expected_kv_regions = array('keys' => $expected_kv_regions_keys,
				'values' => $expected_kv_regions_values);
		$expected_region_input  = 'region_input';
		$expected_game_type_keys = 'game_type_keys';
		$expected_game_type_values = 'game_type_values';
		$expected_game_name_input = 'game_name_input';

		$expected_item = array('body' => $expected_item_body,
				'title' => $expected_item_title,
				'region_id' => $expected_item_region_id);

		$expected_description_input = 'description_input';

		$expected_inputs = array($expected_title_input,
				$expected_info_input,
				$expected_search_offer_input,
				$expected_item_name_input,
				$expected_example_input,
				$expected_game_id_input,
				$expected_game_input,
				$expected_game_name_input,
				$expected_region_input,
				$expected_description_input);

		$_POST["game_id"] = $expected_game_id_post;

		$page = new Page();
		$ad = new Ad($page);

		$mock_toolbox = $this->getMock('Toolbox');
		$mock_toolbox->expects($this->once())
				->method('get_keys_and_values')
				->with("game_type_id", "game_type", "GAME_TYPE")
				->will($this->returnValue(array('keys' => $expected_game_type_keys, 
						'values' => $expected_game_type_values)));
		$ad->set_toolbox($mock_toolbox);

		$mock_game_manager = $this->getMock('GameManager');
		$mock_game_manager->expects($this->once())
				->method('game_get_games_kv')
				->will($this->returnValue($expected_kv_games));
		$ad->set_game_manager($mock_game_manager);

		$mock_region_manager = $this->getMock('RegionManager');
		$mock_region_manager->expects($this->once())
				->method('region_get_regions_kv')
				->with()
				->will($this->returnValue($expected_kv_regions));
		$ad->set_region_manager($mock_region_manager);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_desqlize')
				->with($expected_item_body)
				->will($this->returnValue($expected_desqlized_item_body));
		$ad->set_db_accessor($mock_db_access);

		$mock_form_creator = $this->getMock('FormCreator');
		$mock_form_creator->expects($this->at(0))
				->method('input_field')
				->with("Titel", "title", $expected_item_title, 20, 100, "", 1)
				->will($this->returnValue($expected_title_input));
		$mock_form_creator->expects($this->at(1))
				->method('info')
				->with("De titel wordt automatisch ingevuld.\n" .
						"		<script type=\"text/javaScript\">\n" .
						"			function setTitle() {\n" .
						"				if (document.itemform.seek[0].checked)\n" .
						"					var str = 'GZ';\n" . 
						"				else\n" .
						"					var str = 'TK';\n" .
						"				str += ': ' + document.itemform.what.value;\n" .
						"				document.itemform.title.value = str;\n" .
						"			}\n" .
						"		</script>")
				->will($this->returnValue($expected_info_input));
		$mock_form_creator->expects($this->at(2))
				->method('input_radio')
				->with("", "seek", array("GZ","TK"), array("zoeken", "aanbieden"), "GZ", "setTitle()")
				->will($this->returnValue($expected_search_offer_input));
		$mock_form_creator->expects($this->at(3))
				->method('input_field')
				->with("Wat", "what", substr($expected_item_title, 4), 20, 100, "setTitle()")
				->will($this->returnValue($expected_item_name_input));
		$mock_form_creator->expects($this->at(4))
				->method('info')
				->with("bvb: 'spelers', 'regelboek 4e editie', 'dobbelstenen' ...")
				->will($this->returnValue($expected_example_input));
		$mock_form_creator->expects($this->at(5))
				->method('input_select')
				->with("Spel", 
						"game_id", 
						$expected_game_kv_keys, 
						$expected_game_kv_values, 
						"kies een spel", 
						"NONE", 
						"document.itemform.submit()")
				->will($this->returnValue($expected_game_id_input));
		$mock_form_creator->expects($this->at(6))
				->method('input_select')
				->with("Spel - soort", "game_type", $expected_game_type_keys, $expected_game_type_values)
				->will($this->returnValue($expected_game_input));
		$mock_form_creator->expects($this->at(7))
				->method('input_field')
				->with('Spel - naam', 'game_name')
				->will($this->returnValue($expected_game_name_input));
		$mock_form_creator->expects($this->at(8))
				->method('input_select')
				->with("Regio", "region", 
					array_merge(array("NEW"), $expected_kv_regions_keys), 
					array_merge(array("NIEUWE REGIO"), $expected_kv_regions_values), 
					"kies een regio", 
					$expected_item_region_id, 
					"document.itemform.submit()")
				->will($this->returnValue($expected_region_input));
		$mock_form_creator->expects($this->at(9))
				->method('input_text')
				->with("Beschrijving", "body", $expected_desqlized_item_body)
				->will($this->returnValue($expected_description_input));
		$ad->set_form_creator($mock_form_creator);
		
		$actual = $ad->prepare(null, $expected_item, array());
		$this->assertEquals($expected_inputs, $actual);
	}


	function test_prepare_with_new_region() {
		$expected_kv_games = array('keys' => array(),
				'values' => array());

		$expected_inputs = array();
		$expected_desqlized_item_body = 'desqlized_item_body';
		$expected_item_body = 'item_body';
		$expected_item_title = 'item_title';
		$expected_title_input = 'title_input';
		$expected_info_input = 'info_input';
		$expected_search_offer_input = 'search_offer_input';
		$expected_item_name_input = 'item_name_input';
		$expected_example_input = 'example_input';
		$expected_game_id_input = 'game_id_input';
		$expected_game_kv_keys = array('NONE', 'NEW');
		$expected_game_kv_values = array('GEEN', 'NIEUW SPEL');
		$expected_item_region_id = 5;
		$expected_kv_regions_keys = array('region_keys');
		$expected_kv_regions_values = array('region_values');
		$expected_kv_regions = array('keys' => $expected_kv_regions_keys,
				'values' => $expected_kv_regions_values);
		$expected_region_input  = 'region_input';
		$expected_region_id_post = 'NEW';

		$expected_item = array('body' => $expected_item_body,
				'title' => $expected_item_title,
				'region_id' => $expected_item_region_id);

		$expected_description_input = 'description_input';
		$expected_region_name_input = 'region_name_input';

		$_POST['region'] = $expected_region_id_post;

		$expected_inputs = array($expected_title_input,
				$expected_info_input,
				$expected_search_offer_input,
				$expected_item_name_input,
				$expected_example_input,
				$expected_game_id_input,
				$expected_region_name_input,
				$expected_region_input,
				$expected_description_input);

		$page = new Page();
		$ad = new Ad($page);

		$mock_toolbox = $this->getMock('Toolbox');
		$mock_toolbox->expects($this->once())
				->method('get_keys_and_values')
				->with("game_type_id", "game_type", "GAME_TYPE")
				->will($this->returnValue(array('keys' => null, 'values' => null)));
		$ad->set_toolbox($mock_toolbox);

		$mock_game_manager = $this->getMock('GameManager');
		$mock_game_manager->expects($this->once())
				->method('game_get_games_kv')
				->will($this->returnValue($expected_kv_games));
		$ad->set_game_manager($mock_game_manager);

		$mock_region_manager = $this->getMock('RegionManager');
		$mock_region_manager->expects($this->once())
				->method('region_get_regions_kv')
				->with(1)
				->will($this->returnValue($expected_kv_regions));
		$ad->set_region_manager($mock_region_manager);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_desqlize')
				->with($expected_item_body)
				->will($this->returnValue($expected_desqlized_item_body));
		$ad->set_db_accessor($mock_db_access);

		$mock_form_creator = $this->getMock('FormCreator');
		$mock_form_creator->expects($this->at(0))
				->method('input_field')
				->with("Titel", "title", $expected_item_title, 20, 100, "", 1)
				->will($this->returnValue($expected_title_input));
		$mock_form_creator->expects($this->at(1))
				->method('info')
				->with("De titel wordt automatisch ingevuld.\n" .
						"		<script type=\"text/javaScript\">\n" .
						"			function setTitle() {\n" .
						"				if (document.itemform.seek[0].checked)\n" .
						"					var str = 'GZ';\n" . 
						"				else\n" .
						"					var str = 'TK';\n" .
						"				str += ': ' + document.itemform.what.value;\n" .
						"				document.itemform.title.value = str;\n" .
						"			}\n" .
						"		</script>")
				->will($this->returnValue($expected_info_input));
		$mock_form_creator->expects($this->at(2))
				->method('input_radio')
				->with("", "seek", array("GZ","TK"), array("zoeken", "aanbieden"), "GZ", "setTitle()")
				->will($this->returnValue($expected_search_offer_input));
		$mock_form_creator->expects($this->at(3))
				->method('input_field')
				->with("Wat", "what", substr($expected_item_title, 4), 20, 100, "setTitle()")
				->will($this->returnValue($expected_item_name_input));
		$mock_form_creator->expects($this->at(4))
				->method('info')
				->with("bvb: 'spelers', 'regelboek 4e editie', 'dobbelstenen' ...")
				->will($this->returnValue($expected_example_input));
		$mock_form_creator->expects($this->at(5))
				->method('input_select')
				->with("Spel", 
						"game_id", 
						$expected_game_kv_keys, 
						$expected_game_kv_values, 
						"kies een spel", 
						"NONE", 
						"document.itemform.submit()")
				->will($this->returnValue($expected_game_id_input));
		$mock_form_creator->expects($this->at(6))
				->method('input_field')
				->with('Regio', 'region_name')
				->will($this->returnValue($expected_region_name_input));
		$mock_form_creator->expects($this->at(7))
				->method('input_select')
				->with("Locatie", "region", 
						$expected_kv_regions_keys, 
						$expected_kv_regions_values, 
						"kies een regio", 
						$expected_item_region_id)
				->will($this->returnValue($expected_region_input));
		$mock_form_creator->expects($this->at(8))
				->method('input_text')
				->with("Beschrijving", "body", $expected_desqlized_item_body)
				->will($this->returnValue($expected_description_input));
		$ad->set_form_creator($mock_form_creator);
		
		$actual = $ad->prepare(null, $expected_item, array());
		$this->assertEquals($expected_inputs, $actual);
	}
}

class AdTestClass extends Ad
{
	public function derive_info_test_function() {
		return $this->derive_info();
	}

	public function get_sql_from_post_test_function($item_id) {
		return $this->get_sql_from_post($item_id);
	}
}

?>
